﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TM.QWeibo.Client.UserControls
{
    public partial class HuaTi : UserControl
    {
        private List<SDK.Objects.HuaTi> _huati;
        public HuaTi()
        {
            InitializeComponent();
            Dal.HuaTi.Instance.Trend((rs)=>_huati=rs);
        }

        public Action<string> Selected { get; set; }

        /// <summary>
        /// 屏幕尺寸
        /// </summary>
        private Size renderSize
        {
            get
            {
                return App.Current.RootVisual.RenderSize;
            }
        }

        /// <summary>
        /// 切换显示
        /// </summary>
        public void ToggleShow()
        {
            BindDate();
            Dispatcher.BeginInvoke(() =>
            {

                UserListGrid.Width = renderSize.Width;
                UserListGrid.Height = renderSize.Height;
                txtHuati.Text = "";
                popUserList.IsOpen = !popUserList.IsOpen;
                PopupEffect(popUserList.IsOpen);
            });
        }

        /// <summary>
        /// 显示
        /// </summary>
        public void Show()
        {
            BindDate();
            Dispatcher.BeginInvoke(() =>
            {
                UserListGrid.Width = renderSize.Width;
                UserListGrid.Height = renderSize.Height;
                txtHuati.Text = "";
                popUserList.IsOpen = true;
                var ht = Helper.HuaTiHelper.GetRecent(5);
                if(ht.Count<10)
                {
                    foreach (var huaTi in _huati)
                    {
                        if(ht.Count>=10)
                        {
                            break;
                        }
                        ht.Add(string.Format("#{0}#",huaTi.Name));
                    }
                }
                ht.Reverse();
                HuatiListBox.ItemsSource = ht;
                PopupEffect(popUserList.IsOpen);
            });
        }

        private void PopupEffect(bool isOpen)
        {
            if (isOpen)
            {
                PopupOnStoryboard.Begin();
            }
            else
            {
                PopupOffStoryboard.Begin();
            }
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        public void Hide()
        {
            BindDate();
            Dispatcher.BeginInvoke(() =>
            {
                popUserList.IsOpen = false;
                txtHuati.Text = "";
                PopupEffect(popUserList.IsOpen);
            });
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindDate() {
            Dispatcher.BeginInvoke(() =>
            {
                if (popUserList.IsOpen)
                {
                    HuatiListBox.ItemsSource = Helper.HuaTiHelper.GetRecent(5);
                }
            });
        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            var huati = txtHuati.Text;
            if (!string.IsNullOrWhiteSpace(huati)) {
                var ht = "#" + huati + "#";
                Helper.HuaTiHelper.SetOneHuaTi(ht);
                SelectHuaTi(ht);
            }
        }

        private void HuatiListBox_Tap(object sender, GestureEventArgs e)
        {
            var huati = (string)HuatiListBox.SelectedValue;
            if (!string.IsNullOrWhiteSpace(huati))
            {
                SelectHuaTi(huati);
            }
        }

        private void SelectHuaTi(string huati) {
            if (Selected != null) {
                Selected(huati);
            }
        }

        private void txtKeyword_GotFocus(object sender, RoutedEventArgs e)
        {
            txtHuati.Background = new SolidColorBrush(Color.FromArgb(255, 239, 235, 239));
            txtHuati.BorderThickness = new Thickness(1);
            txtHuati.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148));
        }

        private void txtKeyword_LostFocus(object sender, RoutedEventArgs e)
        {
            txtHuati.Background = new SolidColorBrush(Color.FromArgb(255, 239, 239, 239));
            txtHuati.BorderThickness = new Thickness(1);
            txtHuati.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148));
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Hide();
        }

        private void txtHuati_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnInsert.Focus();
                btnInsert_Click(null, new RoutedEventArgs());
            }
        }
    }
}
